package com.lm.survey.util;

import com.lm.survey.logic.MP3DataBase;
import com.lm.survey.model.MP3File;

public class HTMLContentParse {
    static String ctx = Config.instance().getStringProperty("WEBContextPath");
    public static String getContent(String str){
        int start = 0;
        int index = 0;
        StringBuilder sb = new StringBuilder();
        while((index=str.indexOf("{@",start))!=-1){
            
            int end = str.indexOf("@}",index+2);
            String key = str.substring(index+2,end);
            System.out.println(key);
            
            if(end==-1){
                continue;
            }
            sb.append(str.subSequence(start,index));
            sb.append(getValue(key));            
            start = end+2;
          
            
        }
        sb.append(str.substring(start));
        return sb.toString();
        
        
    }
    private static String getValue(String key){
        
        MP3File file = MP3DataBase.instance().getFile(key);
        if(file.getType().equals("0")){
            return createJPG(file);
        }else if(file.getType().equals("1")){
            return createMP3(file);
        }
        return "test.mp3";
    }
    private static String createJPG(MP3File file){
        String msg = "<img src='"+ctx+"/getfile/"+file.getFileName()+ "' width='400' heigth='300' />";
        return msg;
    }
    
    private static String createMP3(MP3File file){
        String msg = "<EMBED src='"+ctx+"/getfile/"+file.getFileName()
            +"' width='300' height='50' border=0 autostart='false'  loop='ture'></EMBED>";
        return msg;
    }
    
    public static void  main(String[] args){
        MP3DataBase.instance().load();
        String reg = "{@.+@}";
        String  str = "hello this is {@Winter.jpg@},kkwc {@zz66.txt@} hello go";
        System.out.println(getContent(str));
        
        
    }
}
